package com.aviccxzx.smarthome.netutil.app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.aviccxzx.smarthome.netutil.Util;
import com.aviccxzx.smarthome.netutil.XLog;
import com.aviccxzx.smarthome.netutil.sql.KvDb;
import com.aviccxzx.smarthome.netutil.sql.Table;

import org.json.JSONObject;

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        XLog.d("AlarmReceiver.onReceive ");

        if (intent.getCategories().contains(AlarmUtil.CATEGORY)) {
            long id = Long.valueOf(intent.getAction());
            KvDb db = new KvDb(context, AlarmUtil.ALARM_DB);
            Table t = db.tab(AlarmUtil.ALARM_TABLE);
            String s = t.get(""+id);
            if(Util.notEmpty(s)) {
                try {
                    JSONObject obj = new JSONObject(s);
                    JSONObject args = obj.optJSONObject(AlarmUtil.ARGS);
                    long atTime = obj.getLong(AlarmUtil.AT_TIME);
                    String cls = obj.getString(AlarmUtil.CLS);
                    AlarmCallback callback = (AlarmCallback) Class.forName(cls).newInstance();
                    callback.onAlarm(id, atTime, args);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            t = null;
            db = null;
        }
    }
}

